import { createRouter, createWebHistory } from 'vue-router'
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'
import DepartmentList from '../views/DepartmentList.vue'
import DoctorList from '../views/DoctorList.vue'
import DoctorDetail from '../views/DoctorDetail.vue'
import Appointment from '../views/Appointment.vue'
import MyAppointments from '../views/MyAppointments.vue'
import AIQuestion from '../views/AIQuestion.vue'
import Profile from '../views/Profile.vue'
import Admin from '../views/Admin.vue'
import DoctorPatients from '../views/DoctorPatients.vue'

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/home',
    name: 'Home',
    component: Home
  },
  {
    path: '/departments',
    name: 'DepartmentList',
    component: DepartmentList
  },
  {
    path: '/doctors/:departmentId',
    name: 'DoctorList',
    component: DoctorList
  },
  {
    path: '/doctor/:doctorId',
    name: 'DoctorDetail',
    component: DoctorDetail
  },
  {
    path: '/appointment/:doctorId',
    name: 'Appointment',
    component: Appointment
  },
  {
    path: '/my-appointments',
    name: 'MyAppointments',
    component: MyAppointments
  },
  {
    path: '/ai-question',
    name: 'AIQuestion',
    component: AIQuestion
  },
  {
    path: '/profile',
    name: 'Profile',
    component: Profile
  },
  {
    path: '/admin',
    name: 'Admin',
    component: Admin
  },
  {
    path: '/doctor-patients',
    name: 'DoctorPatients',
    component: DoctorPatients
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const isAuthenticated = localStorage.getItem('user')
  
  if (to.path !== '/login' && !isAuthenticated) {
    next('/login')
  } else if (to.path === '/login' && isAuthenticated) {
    next('/home')
  } else {
    next()
  }
})

export default router 